package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Goal;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class GoalDao {

	DBHelp<Goal> db = new DBHelp<Goal>();
	
	public void save(Goal goal) {
		String sql = "INSERT INTO t_goal(gotitle, content, t_project_proid, t_user_useid) VALUE(?,?,?,?)";
		db.executeSQL(sql, goal.getGoal_title(), goal.getGoal_context(), goal.getPro_id(), goal.getUserid());
	}

	public List<Goal> findGoalList() {
		String sql = "SELECT id, gotitle, content, t_project_proid, t_user_useid, gocreatetime FROM t_goal";
		return db.executeQueryToList(sql, new GoalRowMapper());
	}
	
	public Goal findGoalById(String goalId) {
		String sql = "SELECT id, gotitle, content, t_project_proid, t_user_useid, gocreatetime FROM t_goal where id = ?";
		return db.executeQueryToObject(sql, new GoalRowMapper(), goalId);
	}
	
	public void updateGoalById(String goalId, String title, String content) {
		String sql = "UPDATE t_goal SET gotitle = ?, content = ? WHERE id = ?";
		db.executeSQL(sql, title, content, goalId);
	}
	
	private class GoalRowMapper implements RowMapper<Goal>{

		@Override
		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			goal.setGoal_id(rs.getString("id"));
			goal.setGoal_title(rs.getString("gotitle"));
			goal.setGoal_context(rs.getString("content"));
			goal.setPro_id(rs.getString("t_project_proid"));
			goal.setUserid(rs.getString("t_user_useid"));
			goal.setGoal_createtime(rs.getString("gocreatetime"));
			return goal;
		}
		
	}

	public void delGoalById(String goalId) {
		String sql = "DELETE FROM t_goal WHERE id = ?";
		db.executeSQL(sql, goalId);
	}
	
	
	

}
